package com.damie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.dadie.Domain.WasteDomain;
import com.dadie.Dto.WasteDto;
import com.dadie.parame.WasteParame;
import com.dadie.parame.WasteParameUpdate;
import com.damie.mapper.WasteMapper;
import com.damie.service.WasteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Alon
 * @since 2021-01-22
 */
@Service
public class WasteServiceImpl extends ServiceImpl<WasteMapper, WasteDomain> implements WasteService {

    @Resource
    private WasteMapper mapper;

    @Override
    public WasteDto siteAddTypeBysiteId(String site, WasteParame wasteParame) {
        //新增指定站点的指定某类商品类别
        if (wasteParame.getSiteType()==1){
            WasteDto add = add(site, wasteParame);
            return add;
        }else {
            //新增特殊商品 如手机 设定为价格面议，单位为其他
            wasteParame.setWastePrice(new BigDecimal("0"));
            wasteParame.setWeight("特殊");
            WasteDto add = add(site, wasteParame);
            return add;
        }
    }
    public WasteDto add(String site, WasteParame wasteParame){
        //判断当前数据库内是否已有相同的分类数据有则修改无则新增
        QueryWrapper<WasteDomain> rapper = new QueryWrapper<>();
        rapper.eq("site_id",site);
        rapper.eq("waste_name",wasteParame.getWasteName());
        rapper.eq("waste_level",wasteParame.getWasteLevel());
        WasteDomain wasteDomain1 = mapper.selectOne(rapper);
        //判断普通类别等级
        if (wasteParame.getWasteLevel()==2&&!(wasteParame.getParentId().equals("0"))){
            //当前为2级分类
            QueryWrapper<WasteDomain> wrapper = new QueryWrapper<>();
            wrapper.eq("waste_level",1);
            wrapper.eq("waste_id",wasteParame.getParentId());
            wrapper.eq("site_id",site);
            WasteDomain wasteDomain2 = mapper.selectOne(wrapper);
            if (ObjectUtils.isEmpty(wasteDomain2)){
                throw new RuntimeException("设置2级但是你的父级id输入不正确");
            }
            if (ObjectUtils.isEmpty(wasteDomain1)){
                //如果为空说明没有 而且输入的父id也有 执行新增
                WasteDomain wasteDomain = new WasteDomain();
                BeanUtils.copyProperties(wasteParame,wasteDomain);
                wasteDomain.setWasteStetus(1);
                wasteDomain.setSiteId(site);
                int insert = mapper.insert(wasteDomain);
                WasteDto wasteDto = new WasteDto();
                BeanUtils.copyProperties(wasteDomain,wasteDto);
                return wasteDto;
            }else {
                //里面有东西说明有了分类执行修改
                BeanUtils.copyProperties(wasteParame,wasteDomain1);
                int i = mapper.updateById(wasteDomain1);
                WasteDto wasteDto = new WasteDto();
                BeanUtils.copyProperties(wasteDomain1,wasteDto);
                return wasteDto;
            }
        }else if (wasteParame.getWasteLevel()==1&&wasteParame.getParentId().equals("0")){
            //执行一级分类修改新增
            if (ObjectUtils.isEmpty(wasteDomain1)){
                //如果为空说明没有 执行新增
                WasteDomain wasteDomain = new WasteDomain();
                BeanUtils.copyProperties(wasteParame,wasteDomain);
                wasteDomain.setWasteStetus(1);
                wasteDomain.setSiteId(site);
                mapper.insert(wasteDomain);
                WasteDto wasteDto = new WasteDto();
                BeanUtils.copyProperties(wasteDomain,wasteDto);
                return wasteDto;
            }else {
                //里面有东西说明有了分类执行修改
                BeanUtils.copyProperties(wasteParame,wasteDomain1);
                int i = mapper.updateById(wasteDomain1);
                WasteDto wasteDto = new WasteDto();
                BeanUtils.copyProperties(wasteDomain1,wasteDto);
                return wasteDto;
            }
        }
        throw new RuntimeException("参数异常");
    }

    @Override
    public WasteDto updateTypeBySiteId(String site, WasteParameUpdate wasteParame) {
        WasteDomain wasteDomain = mapper.selectById(wasteParame);
        if (ObjectUtils.isEmpty(wasteDomain)){
            throw new RuntimeException("id异常");
        }
        //判读修改的是一级还是二级
        if (wasteParame.getWasteLevel()==1) {
            if (!wasteParame.getParentId().equals("0")) {
                throw new RuntimeException("修改一级，参数ParentId错误");
            }

            BeanUtils.copyProperties(wasteParame,wasteDomain);
            mapper.updateById(wasteDomain);
            WasteDto wasteDto = new WasteDto();
            BeanUtils.copyProperties(wasteDomain,wasteDto);
            return wasteDto;
        }else if (wasteParame.getWasteLevel()==2) {
            //修改二级则判断是否有输入的一级id是否正确
            QueryWrapper<WasteDomain> wrapper = new QueryWrapper<>();
            wrapper.eq("waste_level",1);
            wrapper.eq("waste_id",wasteParame.getParentId());
            wrapper.eq("site_id",site);
            WasteDomain wasteDomain1 = mapper.selectOne(wrapper);
            if (ObjectUtils.isEmpty(wasteDomain1)){
                throw new RuntimeException("参数不正确，修改输入的父类id不存在");
            }
            QueryWrapper<WasteDomain> wrappers = new QueryWrapper<>();
            wrappers.eq("waste_level",2);
            wrappers.eq("waste_id",wasteParame.getWasteId());
            wrappers.eq("site_id",site);
            WasteDomain wasteDomain3 = mapper.selectOne(wrappers);
            if (ObjectUtils.isEmpty(wasteDomain3)){
                throw new RuntimeException("参数不正确，没有找到可修改二级id");
            }
            BeanUtils.copyProperties(wasteParame,wasteDomain1);
            mapper.updateById(wasteDomain1);
            WasteDto wasteDto = new WasteDto();
            BeanUtils.copyProperties(wasteDomain1,wasteDto);
            return wasteDto;
        }
        throw new RuntimeException("输出未知等级");
    }

    @Override
    public WasteDto deleteTypeBySiteId(String site, String wasteId) {
        QueryWrapper<WasteDomain> wrapper = new QueryWrapper<>();
        wrapper.eq("site_id",site);
        wrapper.eq("waste_id",wasteId);
        WasteDomain wasteDomain = mapper.selectOne(wrapper);
        if (ObjectUtils.isEmpty(wasteDomain)){
            throw new RuntimeException("参数出错，查询不到删除对象");
        }
        Integer wasteLevel = wasteDomain.getWasteLevel();
        if (wasteLevel==1){
            QueryWrapper<WasteDomain> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("parent_id",wasteDomain.getWasteId());
            wrapper1.eq("site_id",site);
            wrapper1.eq("waste_level",2);
            List<WasteDomain> wasteDomains = mapper.selectList(wrapper1);
            for (WasteDomain wast:wasteDomains){
                mapper.deleteById(wast.getWasteId());
            }
            mapper.deleteById(wasteId);
            WasteDto wasteDto = new WasteDto();
            BeanUtils.copyProperties(wasteDomain,wasteDto);
            return wasteDto;
        }else {
            mapper.deleteById(wasteId);
            WasteDto wasteDto = new WasteDto();
            BeanUtils.copyProperties(wasteDomain,wasteDto);
            return wasteDto;
        }
    }

    @Override
    public List<WasteDto> selectTypeBySiteId(String site) {
        QueryWrapper<WasteDomain> wrapper = new QueryWrapper<>();
        wrapper.eq("site_id",site);
        wrapper.eq("waste_level",1);
        List<WasteDomain> wasteDomains = mapper.selectList(wrapper);
        List<WasteDto> collect = wasteDomains.stream().map(wasteDomain -> {
            WasteDto wasteDto = new WasteDto();
            BeanUtils.copyProperties(wasteDomain, wasteDto);
            return wasteDto;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<WasteDto> selectTypeBySiteId2(String site, String parentId) {
        QueryWrapper<WasteDomain> wrapper = new QueryWrapper<>();
        wrapper.eq("site_id",site);
        wrapper.eq("waste_level",2);
        wrapper.eq("parent_id",parentId);
        List<WasteDomain> wasteDomains = mapper.selectList(wrapper);
        List<WasteDto> collect = wasteDomains.stream().map(wasteDomain -> {
            WasteDto wasteDto = new WasteDto();
            BeanUtils.copyProperties(wasteDomain, wasteDto);
            return wasteDto;
        }).collect(Collectors.toList());
        return collect;
    }
}

